/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} list1
 * @param {number} a
 * @param {number} b
 * @param {ListNode} list2
 * @return {ListNode}
 */
var mergeInBetween = function (list1, a, b, list2) {
  let pRight = list1, pLeft;
  let i = 0;
  //找到删除节点的两端节点
  while (i <= b) {
    if (i === (a - 1)) {
      pLeft = pRight;
    }
    pRight = pRight.next;
    i++;
  }
  //1.将左节点的指针与list2的头节点连接
  pLeft.next = list2;

  //2.遍历list2的最右结点
  while (list2.next) {
    list2 = list2.next;
  }

  //3.将list2右结点指针与删除节点的右结点连接
  list2.next = pRight;
  return list1;
};